"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 发布订阅
Case Name   : gs_probackup本地备份订阅端
Create At   : 2022/11/09
Owner       : opentestcase033
Description :
    1.在两个集群创建表
    2.订阅端打开增量备份开关
    3.创建发布订阅
    4.订阅端全量备份
    5.发布端表中插入数据
    6.订阅端查询表数据是否更新
    7.修改订阅端
    8.增量备份订阅端
    9.使用全量备份恢复订阅端
    10.发布端修改表数据，订阅端查询
    11.使用step8中的增量备份恢复订阅端
    12.发布端修改表数据，订阅端查询
    13.删除订阅端
    14.使用step8中的增量备份恢复订阅端
    15.发布端修改表数据，订阅端查询
    16.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
    6.数据更新
    7.成功
    8.成功
    9.成功
    10.数据同步，订阅binary为false
    11.成功
    12.数据同步，订阅binary为true
    13.成功
    14.成功
    15.数据不同步，订阅端主备数据一致
    16.成功
History     :
"""

import os
import time
import unittest
from yat.test import macro
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '非1+2环境不执行')
class Pubsubclass(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_userdb_pub = Node(node='PrimaryDbUser')
        self.pri_userdb_sub = Node(node='remote1_PrimaryDbUser')
        self.constant = Constant()
        self.commsh_pub = CommonSH('PrimaryDbUser')
        self.commsh_sub_std = [CommonSH('remote1_Standby1DbUser'),
                               CommonSH('remote1_Standby2DbUser')]
        self.commsh_sub = CommonSH('remote1_PrimaryDbUser')
        self.com_pub = Common()
        self.com_sub = Common('remote1_PrimaryDbUser')
        self.schema = 's_pubsub_0212'
        self.tablespace = 'tbs_pubsub_0212'
        self.tb_name1 = 't_pubsub_0212_1'
        self.tb_name2 = 't_pubsub_0212_2'
        self.subname = "sub_pubsub_0212"
        self.pubname = "pub_pubsub_0212"
        self.path1 = os.path.join(
            os.path.dirname(macro.DB_INSTANCE_PATH), 'tablespace_0212')
        self.path2 = os.path.join(
            os.path.dirname(macro.DB_INSTANCE_PATH_REMOTE1), 'tablespace_0212')
        self.basebackup = os.path.join(
            os.path.dirname(macro.DB_INSTANCE_PATH_REMOTE1), 'basebackup')
        self.port = str(int(self.pri_userdb_pub.db_port) + 1)
        self.wal_level = self.com_pub.show_param("wal_level")
        self.enable_cbm_tracking = self.com_sub.show_param(
            "enable_cbm_tracking", env_path=macro.DB_ENV_PATH_REMOTE1)
        self.user_param_pub = f'-U {self.pri_userdb_pub.db_user} ' \
                              f'-W {self.pri_userdb_pub.db_password}'
        self.user_param_sub = f'-U {self.pri_userdb_sub.db_user} ' \
                              f'-W {self.pri_userdb_sub.db_password}'
        text = '-----查询集群状态，配置白名单，发布端设置wal_level=logical-----'
        self.log.info(text)
        self.log.info('-----发布端状态-----')
        status = self.commsh_pub.get_db_cluster_status('detail')
        self.log.info(status)
        self.assertEqual(status.count('Normal'), 4, f'执行失败:{text}')
        self.log.info('-----订阅端状态-----')
        status = self.commsh_sub.get_db_cluster_status(
            'detail', env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(status)
        self.assertEqual(status.count('Normal'), 4, f'执行失败:{text}')
        guc_res = self.commsh_pub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host  replication  {self.pri_userdb_sub.db_user}  '
            f'{self.pri_userdb_sub.db_host}/32  sha256')
        self.log.info(guc_res)
        self.assertTrue(guc_res, f'发布端配置白名单失败:{text}')
        if 'logical' != self.wal_level:
            result = self.commsh_pub.execute_gsguc(
                'set', self.constant.GSGUC_SUCCESS_MSG, 'wal_level=logical')
            self.assertTrue(result, f'设置wal_level失败:{text}')
            restart_res = self.commsh_pub.restart_db_cluster()
            self.assertTrue(restart_res, f'重启数据库失败:{text}')
        guc_res = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, macro.DB_INSTANCE_PATH_REMOTE1,
            f'host  replication  {self.pri_userdb_pub.db_user}  '
            f'{self.pri_userdb_pub.db_host}/32  sha256',
            macro.DB_ENV_PATH_REMOTE1)
        self.log.info(guc_res)
        self.assertTrue(guc_res, f'订阅端配置白名单失败:{text}')

    def test_pub_sub(self):
        text = "-----step1:在两个集群创建模式、表空间和表;expect:成功-----"
        self.log.info(text)
        sql = """drop schema if exists {0} cascade;
        create schema {0};
        drop tablespace if exists {1};
        create tablespace {1} location '{2}';
        drop table if exists {0}.{3};
        drop table if exists {4};
        create table {0}.{3}(id1 int primary key, id2 int, id3 int);
        create table {4}(id int primary key constraint id_nn not null,
        use_filename varchar2(20),filename varchar2(255),
        text varchar2(2000)) tablespace {1} partition by range(id)
        ( partition p1 values less than(30),
          partition P2 values less than(60),
          partition P3 values less than(90),
          partition P4 values less than(MAXVALUE));"""
        pub_res = self.commsh_pub.execut_db_sql(
            sql.format(self.schema, self.tablespace, self.path1, self.tb_name1,
                       self.tb_name2),
            sql_type=self.user_param_pub)
        self.log.info(pub_res)
        self.assertIn(self.constant.CREATE_SCHEMA_SUCCESS_MSG, pub_res,
                      f"执行失败:{text}")
        self.assertIn(self.constant.TABLESPCE_CREATE_SUCCESS, pub_res,
                      f"执行失败:{text}")
        self.assertEqual(pub_res.count(self.constant.TABLE_CREATE_SUCCESS),
                         5, f"执行失败:{text}")
        sub_res = self.commsh_sub.execut_db_sql(
            sql.format(self.schema, self.tablespace, self.path2, self.tb_name1,
                       self.tb_name2),
            self.user_param_sub,
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_res)
        self.assertIn(self.constant.CREATE_SCHEMA_SUCCESS_MSG, sub_res,
                      f"执行失败:{text}")
        self.assertIn(self.constant.TABLESPCE_CREATE_SUCCESS, sub_res,
                      f"执行失败:{text}")
        self.assertEqual(sub_res.count(self.constant.TABLE_CREATE_SUCCESS),
                         5, f"执行失败:{text}")

        text = "-----step2:订阅端打开增量备份开关;expect:成功-----"
        self.log.info(text)
        result = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            'enable_cbm_tracking=on',
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1,
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertTrue(result, f"执行失败:{text}")

        text = "-----step3:创建发布订阅 expect:成功-----"
        self.log.info(text)
        sql_cmd = f"drop publication if exists {self.pubname};" \
                  f"create publication {self.pubname} for table " \
                  f"{self.schema}.{self.tb_name1},{self.tb_name2};"
        pub_result = self.commsh_pub.execut_db_sql(
            sql_cmd, sql_type=self.user_param_pub)
        self.log.info(pub_result)
        self.assertIn(self.constant.create_pub_succ_msg, pub_result,
                      f"执行失败:{text}")
        result = self.commsh_sub.execute_generate(
            macro.COMMON_PASSWD, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.assertIn(self.constant.create_keycipher_success, result,
                      f"执行失败:{text}")
        sql_cmd = f"drop subscription if exists {self.subname};" \
                  f"create subscription {self.subname} connection " \
                  f"'host={self.pri_userdb_pub.db_host} " \
                  f"port={self.port} user={self.pri_userdb_pub.db_user} " \
                  f"dbname={self.pri_userdb_pub.db_name} " \
                  f"password={self.pri_userdb_pub.ssh_password}' publication" \
                  f" {self.pubname} with(copy_data=false);"
        sub_result = self.commsh_sub.execut_db_sql(
            sql_cmd, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertIn(self.constant.create_sub_succ_msg, sub_result,
                      f"执行失败:{text}")

        text = "-----step4:订阅端全量备份;expect:成功-----"
        self.log.info(text)
        check_cmd = f'if [ -d {self.basebackup} ]; then ' \
                    f'rm -rf {self.basebackup}; fi; mkdir {self.basebackup};'
        self.log.info(check_cmd)
        result = self.pri_userdb_sub.sh(check_cmd).result()
        self.log.info(result)
        gs_probackup_init = self.commsh_sub.exec_pro_backup_init(
            self.basebackup, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(gs_probackup_init)
        self.assertTrue(gs_probackup_init, f"执行失败:{text}")
        gs_probackup_add = self.commsh_sub.exec_pro_back_add(
            self.basebackup, 'instance',
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1,
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(gs_probackup_add)
        self.assertTrue(gs_probackup_add, f"执行失败:{text}")
        gs_probackup = self.commsh_sub.exec_pro_backup_backup(
            self.basebackup, 'instance', 'full', self.pri_userdb_sub.db_name,
            other_cmd=f'--backup-pg-replslot -E {self.path2}',
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(gs_probackup)
        self.assertTrue(gs_probackup, f"执行失败:{text}")

        text = "-----step5:发布端表中插入数据;expect:成功-----"
        self.log.info(text)
        sql = f"insert into {self.schema}.{self.tb_name1} values(1, 1, 1);" \
              f"insert into {self.tb_name2} values(1, 'first', '%一', '');"
        pub_result = self.commsh_pub.execut_db_sql(
            sql, sql_type=self.user_param_pub)
        self.log.info(pub_result)
        self.assertEqual(pub_result.count(self.constant.INSERT_SUCCESS_MSG), 2,
                         f"执行失败:{text}")

        text = "-----step6:订阅端查询数据是否更新 expect:数据更新-----"
        self.log.info(text)
        self.commsh_pub.check_sub_consistency(
            self.subname, self.user_param_pub, 20)
        sql_select = f"select * from {self.schema}.{self.tb_name1};" \
                     f"select * from {self.tb_name2};"
        sub_result = self.commsh_sub.execut_db_sql(
            sql_select, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertIn("1 | first        | %一", sub_result, f"执行失败:{text}")
        self.assertIn("1 |   1 |   1", sub_result, f"执行失败:{text}")

        text = "-----step7:修改订阅端;expect:成功-----"
        self.log.info(text)
        sub_result = self.commsh_sub.execut_db_sql(
            f"alter subscription {self.subname} set (binary=True);",
            sql_type=self.user_param_pub, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertIn(self.constant.alter_sub_succ_msg, sub_result,
                      f"执行失败:{text}")

        text = "-----step8:增量备份订阅端;expect:成功-----"
        self.log.info(text)
        gs_probackup = self.commsh_sub.exec_pro_backup_backup(
            self.basebackup, 'instance', 'PTRACK ',
            self.pri_userdb_sub.db_name,
            other_cmd=f'--backup-pg-replslot -E {self.path2}',
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(gs_probackup)
        self.assertTrue(gs_probackup, f"执行失败:{text}")

        def exec_restore(mode):
            stop_res = self.commsh_sub.stop_db_cluster(
                env_path=macro.DB_ENV_PATH_REMOTE1)
            self.log.info(stop_res)
            self.assertTrue(stop_res, f"执行失败:{text}")
            gs_probackup_show = self.commsh_sub.exec_probackup_show(
                self.basebackup, 'instance', macro.DB_ENV_PATH_REMOTE1)
            info_list = str(gs_probackup_show).splitlines()
            restore_mod = ''
            for i in info_list:
                if mode in str(i):
                    restore_mod = str(str(i).split()[2]).strip()
            gs_restore = self.commsh_sub.exec_pro_backup_restore(
                self.basebackup, 'instance', restore_mod,
                env_path=macro.DB_ENV_PATH_REMOTE1)
            self.log.info(gs_restore)
            self.assertTrue(gs_restore, f"执行失败:{text}")
            start_res = self.commsh_sub.start_db_cluster(
                True, macro.DB_ENV_PATH_REMOTE1)
            self.assertTrue(self.constant.START_SUCCESS_MSG in start_res or
                            'Degrade' in start_res, f"执行失败:{text}")
            for i in self.commsh_sub_std:
                build_res = i.build_standby(
                    "-b full -t 3600", env_path=macro.DB_ENV_PATH_REMOTE1,
                    db_path=macro.DB_INSTANCE_PATH_REMOTE1)
                self.assertIn(self.constant.BUILD_SUCCESS_MSG, build_res,
                              f"执行失败:{text}")
            time.sleep(2)
            sub_status = self.commsh_sub.get_db_cluster_status(
                'detail', env_path=macro.DB_ENV_PATH_REMOTE1)
            self.log.info(sub_status)
            self.assertEqual(sub_status.count('Normal'), 4, f"执行失败:{text}")

        text = "-----step9:使用全量备份恢复订阅端;expect:成功-----"
        self.log.info(text)
        exec_restore('FULL')

        text = "-----step10:发布端修改表数据，订阅端查询;expect:数据同步，订阅binary为false-----"
        self.log.info(text)
        sql = f"insert into {self.schema}.{self.tb_name1} values(2,1,1);" \
              f"insert into {self.tb_name2} values(2,'first','%一','');"
        pub_result = self.commsh_pub.execut_db_sql(
            sql, sql_type=self.user_param_pub)
        self.log.info(pub_result)
        self.assertEqual(pub_result.count(self.constant.INSERT_SUCCESS_MSG), 2,
                         f"执行失败:{text}")
        self.commsh_pub.check_sub_consistency(
            self.subname, self.user_param_pub, 20)
        select_sub = "select subname, subbinary from pg_subscription;"
        sub_res = self.commsh_sub.execut_db_sql(
            sql_select + select_sub, self.user_param_sub,
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_res)
        self.assertIn("2 |   1 |   1", sub_res, f"执行失败:{text}")
        self.assertIn("2 | first        | %一", sub_res, f"执行失败:{text}")
        self.assertEqual(sub_res.count('1 row'), 3, f"执行失败:{text}")
        self.assertIn(f"{self.subname} | f", sub_res, f"执行失败:{text}")

        text = "-----step11:使用step8中的增量备份恢复订阅端;expect:成功-----"
        self.log.info(text)
        exec_restore(mode='PTRACK')

        text = "-----step12:发布端修改数据，订阅端查询;expect:数据同步，订阅binary为true-----"
        self.log.info(text)
        sql = f"update {self.schema}.{self.tb_name1} set id3=999;" \
              f"update {self.tb_name2} set filename='update';"
        pub_result = self.commsh_pub.execut_db_sql(
            sql, sql_type=self.user_param_pub)
        self.log.info(pub_result)
        self.assertEqual(pub_result.count(self.constant.UPDATE_SUCCESS_MSG), 2,
                         f"执行失败:{text}")
        self.commsh_pub.check_sub_consistency(
            self.subname, self.user_param_pub, 20)
        sub_res = self.commsh_sub.execut_db_sql(
            sql_select + select_sub, self.user_param_sub,
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_res)
        self.assertIn("1 |   1 | 999", sub_res, f"执行失败:{text}")
        self.assertIn("1 | first        | update", sub_res, f"执行失败:{text}")
        self.assertIn(f"{self.subname} | t", sub_res, f"执行失败:{text}")

        text = "-----step13:删除订阅端;expect:成功-----"
        self.log.info(text)
        result = self.commsh_sub.execut_db_sql(
            f"drop subscription {self.subname};",
            sql_type=self.user_param_pub,
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertIn(self.constant.drop_sub_succ_msg, result, f"执行失败:{text}")

        text = "-----step14:使用step8中的增量备份恢复订阅端;expect:成功-----"
        self.log.info(text)
        exec_restore(mode='PTRACK')

        text = "-----step15:发布端修改数据，订阅端查询;expect:数据不同步，订阅端主备数据一致-----"
        self.log.info(text)
        pub_res = self.commsh_pub.execut_db_sql(
            f"delete from {self.schema}.{self.tb_name1};"
            f"select * from pg_replication_slots;",
            sql_type=self.user_param_pub)
        self.log.info(pub_res)
        self.assertIn(self.constant.DELETE_SUCCESS_MSG, pub_res,
                      f"执行失败:{text}")
        self.assertNotIn(self.subname, pub_res, f"执行失败:{text}")
        sub_res = self.commsh_sub.execut_db_sql(
            sql_select + select_sub, self.user_param_sub,
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_res)
        sub_std_res = self.commsh_sub_std[0].execut_db_sql(
            sql_select + select_sub, self.user_param_sub,
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(f"订阅备机查询: {sub_std_res}")
        self.assertIn("1 |   1 |   1", sub_res, f"执行失败:{text}")
        self.assertIn("1 | first        | %一", sub_res, f"执行失败:{text}")
        self.assertIn(f"{self.subname} | t", sub_res, f"执行失败:{text}")
        self.assertEqual(sub_res, sub_std_res, f"执行失败:{text}")

    def tearDown(self):
        text = "--step16:清理环境 expect:成功--"
        self.log.info(text)
        drop_sub_res = self.commsh_sub.execut_db_sql(
            f"drop subscription if exists {self.subname};",
            self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(drop_sub_res)
        drop_pub_res = self.commsh_pub.execut_db_sql(
            f"drop publication if exists {self.pubname};",
            sql_type=self.user_param_pub)
        self.log.info(drop_pub_res)
        sql_cmd = f"drop table if exists {self.schema}.{self.tb_name1};" \
                  f"drop table if exists {self.tb_name2};" \
                  f"drop schema if exists {self.schema} cascade;" \
                  f"drop tablespace if exists {self.tablespace};"
        drop_res0 = self.commsh_sub.execut_db_sql(
            sql_cmd, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(drop_res0)
        drop_res1 = self.commsh_pub.execut_db_sql(
            sql_cmd, sql_type=self.user_param_pub)
        self.log.info(drop_res1)
        wal_level_res = self.commsh_pub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'wal_level={self.wal_level}')
        self.log.info(wal_level_res)
        pub_guc_res = self.commsh_pub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host  replication  {self.pri_userdb_sub.db_user}  '
            f'{self.pri_userdb_sub.db_host}/32')
        self.log.info(pub_guc_res)
        restart_res = self.commsh_pub.restart_db_cluster()
        sub_guc_res0 = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, macro.DB_INSTANCE_PATH_REMOTE1,
            f'host  replication  {self.pri_userdb_pub.db_user}  '
            f'{self.pri_userdb_pub.db_host}/32', macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_guc_res0)
        sub_guc_res1 = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            f'enable_cbm_tracking={self.enable_cbm_tracking}',
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1,
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_guc_res1)
        cmd = f"rm -rf {self.path1};"
        self.log.info(cmd)
        sh_result1 = self.pri_userdb_pub.sh(cmd).result()
        self.log.info(sh_result1)
        cmd = f"rm -rf {self.path2};rm -rf {self.basebackup}"
        self.log.info(cmd)
        sh_result2 = self.pri_userdb_sub.sh(cmd).result()
        self.log.info(sh_result2)
        self.assertIn(self.constant.drop_sub_succ_msg, drop_sub_res,
                      f'删除订阅失败:{text}')
        self.assertIn(self.constant.drop_pub_succ_msg, drop_pub_res,
                      f'删除发布失败:{text}')
        self.assertEqual(drop_res0.count(self.constant.DROP_TABLE_SUCCESS), 3,
                         f'删除订阅端表失败:{text}')
        self.assertIn(self.constant.DROP_SCHEMA_SUCCESS_MSG, drop_res0,
                      f"删除订阅端模式失败:{text}")
        self.assertIn(self.constant.TABLESPCE_DROP_SUCCESS, drop_res0,
                      f"删除订阅端表空间失败:{text}")
        self.assertEqual(drop_res1.count(self.constant.DROP_TABLE_SUCCESS), 3,
                         f'删除发布端表失败:{text}')
        self.assertIn(self.constant.DROP_SCHEMA_SUCCESS_MSG, drop_res1,
                      f"删除发布端模式失败:{text}")
        self.assertIn(self.constant.TABLESPCE_DROP_SUCCESS, drop_res1,
                      f"删除发布端表空间失败:{text}")
        self.assertTrue(wal_level_res, f'执行失败:{text}')
        self.assertTrue(restart_res, f'发布端重启数据库失败:{text}')
        self.assertTrue(pub_guc_res, f'发布端删除白名单失败:{text}')
        self.assertTrue(sub_guc_res0 and sub_guc_res1, f'订阅端删除白名单失败:{text}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
